/***********part1:获得ajax通讯对象************
	1. g_createHttpRQ();
*****************************************/
function g_createHttpRQ(){
	var obj = null;
	try{
		obj =  new XMLHttpRequest();
	}
	catch(e){
		try{
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(ee){
			obj = null;
		}
	}
	return obj;
}
/***********part1:获得ajax通讯对象结束************

/***********part2:为标签对象添加事件************
	1. g_addEvent();
	2. g_addIdEvent();
	3. g_addNameEvent();
*****************************************/
/**
 * 功能:为obj组件type事件添加响应函数fn
 * 全局函数
 * 2013-07-12
 */
function g_addEvent(obj, type, fn){
	if(obj && obj.addEventListener){ //W3C
		obj.addEventListener(type, fn, false);
	}
	else{
		if(obj && obj.attachEvent){	//Older IE
			obj.attachEvent("on"+type, fn);
		}
	}		
}

//为id属性对象添加type事件
function g_addIdEvent(id, type, fn){
	var idobj = document.getElementById(id);
	g_addEvent(idobj, type, fn);
}

//为name属性对象添加type事件响应函数fn
//但不是对所有name对应组件有效，要慎重用
function g_addNameEvent(name, type, fn){
	var nameobj = document.getElementsByName(name);
	for(var i=0; i<nameobj.length; i++)
		g_addEvent(nameobj[i], type, fn);
}

/***********part2:为标签对象添加事件结束************/

/********part3:通用的数据校验函数及字符串操作函数******
	1. g_trim();
	2. g_trim_id();
	3. g_trim_name();
	4. g_check_int();
	5. g_check_date();
	6. g_getdate();
	7. g_check_empty();
*******************************************/

//字符串左右去空格函数
function g_trim(s){
	return s.replace(/(^\s*)|(\s*$)/g,"");
}

//获得id标签组件值，并左右去空格
function g_trim_id(id){
	var obj = document.getElementById(id)
	return g_trim(obj.value);
}

//获得name标签对象组件值，仅对数组第1个元素值左右去空格
function g_trim_name(name){
	var obj = document.getElementsByName(name)
	return g_trim(obj[0].value);
}

//检查字符串s是否是整形数
function g_check_int(s)
{   	
	if (/^[0-9]+$/.test( s ) && (s>=0))
	{
	   return true;
	} 
	else 
	{
	    return false;
	}
}

//检查是否为 YYYY-MM-DD || YYYY/MM/DD 的日期格式 
function g_check_date(str){ 
	var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
	if(result==null) return false; 
	var d=new Date(result[1], result[3]-1, result[4]);
	
	return (d.getFullYear()==result[1] && d.getMonth()+1==result[3] && d.getDate()==result[4]); 
} 

//获得用0对齐的日期字符串 YYYY-MM-DD
function g_getdate(str){
	var ary_date = str.split("-");
	var result_date = ary_date[0];
	if(ary_date[1].length==1)
		result_date += "-0"+ary_date[1];
	else
		result_date += "-"+ary_date[1];

	if(ary_date[2].length==1)
		result_date += "-0"+ary_date[2];
	else
		result_date += "-"+ary_date[2];
	
	return result_date;
}

/**
 *检查去空格的str中间有无空格，有返回true
 */
function g_check_empty(str){
	for(var i=0;i<str.length;i++){
		if(str.charAt(i)==' ' || str.charAt(i)=='\t')
			return true;
	}
	return false;
}

/*********part3:通用的数据校验函数及字符串操作函数结束***/


/********part4:JSON字符串解析******************
1. g_parseJSONString();
*******************************************/
function g_parseJSONString(str){
	var obj = eval("(" +str+ ")");
	return obj;
}
/********part4:JSON字符串解析结束****************/


/********part5:在工具条按钮组中设置焦点按钮**********/
/*name: 按钮组name属性值，可获得按钮数组对象
 *obj:  该按钮对象设置成焦点
 *      其它按钮非焦点
 *2019-05-02 
 */
function g_btnFocus(name, obj){
	var objs = document.getElementsByName(name);
	for(var i=0; i<objs.length; i++){
		if(obj == objs[i])
			obj.className = "btn active";
		else
			objs[i].className = "btn";	
	}
}
/********part5:在工具条按钮组中设置焦点按钮结束*******/


/******part6:各种对话框confirm(save,update,del)****
  1. g_alert();
  2. g_confirm();
***********************************************/

/*返回模态对话框对应字符串
 * 有四个模板填充串，需数组传参过来，依次形如下述:
 *    删除确认
 *    icon-warning-sign
 *    Are you sure you want to delete the user?
 *    增加
 *    
 *    参数:info是一个字符串数组
*/
function g_confirmString(info){
	var s = 
		"  <div class='modal-header'>"
		+"    <button type='button' class='close' data-dismiss='modal' aria-hidden='true' id='modal_exit'>×</button>"
		+"    <h3 id='myModalLabel'>&&PARA&&</h3>"
		+"  </div>"
		+"  <div class='modal-body'>"
		+"    "
		+"    <p class='error-text'><i class='&&PARA&& modal-icon'></i>&&PARA&&</p>"
		+"  </div>"
		+"  <div class='modal-footer'>"
		+"    <button class='btn' data-dismiss='modal' aria-hidden='true' id='modal_cancel'>取消</button>"
		+"    <button class='btn btn-danger' data-dismiss='modal' id='modal_app'>&&PARA&&</button>"
		+"  </div>";
	
	for(var i=0; i<info.length; i++){
		s = s.replace("&&PARA&&", info[i]);
	}
	
	return s;
}

/*********************************************
*形如confirm对话框
 * 有四个模板填充串，需数组传参过来，依次形如下述:
 *    删除确认
 *    icon-warning-sign
 *    Are you sure you want to delete the user?
 *    增加
 *    
 *    参数:info是一个字符串数组

*********************************************/
function g_confirm(info){
	var s = g_confirmString(info);
	var obj = document.getElementById("fade");	//禁止UI其它操作
	obj.style.display = "block";	
	obj = document.getElementById("myModal");	//使能对话框操作
	obj.innerHTML = s;
	obj.className = "modal";
	
	//为模态对话框添加事件注册,此两按钮调用共同的函数
	//确认按钮响应事件加在具体的模块中
	g_addIdEvent("modal_exit","click",g_cancelConfirm);
	g_addIdEvent("modal_cancel","click",g_cancelConfirm);	
}

/************取消confirm、alert对话框*************/
function g_cancelConfirm(){
	var obj = document.getElementById("myModal");
	obj.innerHTML = "";
	obj.className = "modal small hide fade";
	
	obj = document.getElementById("fade");
	obj.style.display = "none";
}


function g_alertString(info){
	var s = 
		"  <div class='modal-header'>"
		+"    <button type='button' class='close' data-dismiss='modal' aria-hidden='true' id='modal_exit'>×</button>"
		+"    <h3 id='myModalLabel'>确认</h3>"
		+"  </div>"
		+"  <div class='modal-body'>"
		+"    "
		+"    <p class='error-text'><i class='&&PARA&& modal-icon'></i>&&PARA&&</p>"
		+"  </div>"
		+"  <div class='modal-footer'>"
		+"    <button class='btn' data-dismiss='modal' aria-hidden='true' id='modal_cancel'>确定</button>"
		+"  </div>";
	
	for(var i=0; i<info.length; i++)
		s = s.replace("&&PARA&&", info[i]);
	return s;
}

/*自定义alert对话框
 *与主页面manage.jsp中的 
 *<div id=myModal>
 *<div id="fade">相关
 *
 *参数info:是一个信息字符串数组，长度2
 *info[0]:图标串,icon-ok-sign,icon-warning-sign等
 *info[1]:信息串。若成功与icon-ok-sign配对
*/
function g_alert(info){
	var s = g_alertString(info);	
	var obj = document.getElementById("fade");	//禁止UI其它操作
	obj.style.display = "block";
	obj = document.getElementById("myModal");	//使能对话框操作
	obj.innerHTML = s;
	obj.className = "modal";
	
	g_addIdEvent("modal_exit","click", g_cancelConfirm);
	g_addIdEvent("modal_cancel","click", g_cancelConfirm);	
}

/******part6:各种对话框confirm(save,update,del)结束****/

/******************一般模态功能对话框*******************/
/**
* @param title 	:对话框标题字符串
* @param content:对话框内容字符串
* @param width: 对话框宽度
* @param height: 对话框高度
* @param autoscroll: 对话框垂直滚动
*/

function g_modal(title,content, width, height, autoscroll){
	var s = 
		"  <div class='modal-header'>"
		+"    <button type='button' class='close' data-dismiss='modal' aria-hidden='true' id='modal_exit'>×</button>"
		+"    <h3 id='myModalLabel'>" +title+ "</h3>"
		+" </div>";
	s = s+ content;
	
	var obj = document.getElementById("fade");	//禁止UI其它操作
	obj.style.display = "block";
	
	obj = document.getElementById("myModal");	//使能对话框操作
	obj.style.width = width+"px";
	if(height != null)
		obj.style.height = height+"px";
	obj.style.overflow = autoscroll;
	obj.innerHTML = s;
	obj.className = "modal";
	
	//为模态对话框添加事件注册,此两按钮调用共同的函数
	//确认按钮响应事件加在具体的模块中
	g_addIdEvent("modal_exit","click",g_cancelConfirm);	
}


/******************一般模态功能对话框*******************/

/**
 * 功能:表格通用显示函数
 * @param title :数组,表头内容
 * @param size:表头宽度数组
 * @param align:<td>对齐方式
 * @param data:表头数据数组
 *     (一维数组,行数=data.length/title.size)
 */
function g_getTableHTML(title,size,align,obj){
	var s = 
	"<div class='container-fluid'>"
	+" <div class='row-fluid'>"
	+    "<div class='well'>"
	+      "<table class='table table-striped' style='table-layout: fixed'>"
	+        "<tr>";
	var i;
	for(i=0; i<title.length; i++){
		s += "<th width='" +size[i]+ "'>" +title[i]+ "</th>";
	}
	s += "</tr>";
	var pos = 0;
	var rows = 0;
	rows = obj.row.length;
	
	for(i=0; i<rows; i++){
		s += "<tr>";
		for(var j=0; j<title.length; j++){
			s += "<td>" +obj.row[i].col[j]+ "</td>";
		}
		s += "</tr>";
	}
	s += "</table>";
	s += "</div>";
	s += g_getTableCtrl();
	s += "</div>"
	s += "</div>";
	return s;
}

function g_getCheckTableHTML(title,size,align,data){
	var s = "<div class='well'>";
	var s = "<table class='table'><tr>";
	var i;
	for(i=0; i<title.length; i++){
		s += "<th height='30px' width='" +size[i]+ "'>" +title[i]+ "</th>";
	}
	s += "</tr>";
	
	var pos = 0;
	var rows = 0;
	if(data == null)
		rows = 0;
	else
		rows = data.length/title.length;
	
	var n = 0;
	var mid = "";
	if(rows>0){
		if(data[5].firstChild.nodeValue=='y')
			mid = "#808080";
		else
			mid = "#000000";
	}
	for(i=0; i<rows; i++){
		s += "<tr>";
		for(var j=0; j<title.length; j++){
			if(j<=5){
				if(data[n].firstChild)
					s += "<td style='color:#808080;' height='30px' bgcolor='#FFFFFF' align='" +align[j]+ "'>" +data[n].firstChild.nodeValue+ "</td>";
				else
					s += "<td style='color:#808080;' height='30px' bgcolor='#FFFFFF' align='" +align[j]+ "'></td>";
					
			}
			else{
				if(data[n].firstChild && data[n].firstChild.nodeValue!="null")
					s += "<td style='color:" +mid+ ";' height='30px' bgcolor='#FFFFFF' align='" +align[j]+ "'>" +data[n].firstChild.nodeValue+ "</td>";
				else
					s += "<td style='color:" +mid+ ";' height='30px' bgcolor='#FFFFFF' align='" +align[j]+ "'></td>";					
			}
			n ++;
		}
		s += "</tr>";
	}
	s += "</table>";
	
	return s;
}

function g_getDetailTableHTML(title,size,align,data){
	var s = "<table id='show_table' border='0' cellpadding='0' cellspacing='1' bgcolor='b5d6e6'><tr bgcolor='#ddccff'>";
	var i;
	for(i=0; i<title.length; i++){
		s += "<th height='22px' width='" +size[i]+ "'>" +title[i]+ "</th>";
	}
	s += "</tr>";
	
	var pos = 0;
	var rows = 0;
	if(data == null)
		rows = 0;
	else
		rows = data.length/title.length;
	
	var n = 0;
	for(i=0; i<rows; i++){
		s += "<tr>";
		for(var j=0; j<title.length; j++){
			if(data[n].firstChild)
				s += "<td bgcolor='#FFFFFF' align='" +align[j]+ "'>" +data[n].firstChild.nodeValue+ "</td>";
			else
				s += "<td bgcolor='#FFFFFF' align='" +align[j]+ "'></td>";
				
			n ++;
		}
		s += "<td align='center'><input type='button' value='详细信息' name='detail_btn' /></td>";
		s += "</tr>";
	}
	s += "</table>";
	
	return s;
}

/**
 *功能：表格页面控制函数 
 * @returns {String}
 */
function g_getTableCtrl(){
	g_appObj.totalPage = Math.round(g_appObj.totalSize/g_appObj.pageSize - 0.5);
	if(g_appObj.totalSize%g_appObj.pageSize !=0 )
		g_appObj.totalPage ++;
	
	//每页记录数选择
	var pagerecords = [10,20,30,50]; //数组值1000表明是全部显示的标记
	var pagevalues = ["10","20","30","50"];
	var strpage="<select id='show_pagesize' style='float:left;height:30px;width:60px'>" ;
	for(var i=0; i<pagerecords.length; i++){
		if(g_appObj.pageSize == pagerecords[i])
			strpage += "<option selected value='" +pagerecords[i]+ "'>" +pagevalues[i]+ "</option>";
		else
			strpage += "<option value='" +pagerecords[i]+ "'>" +pagevalues[i]+ "</option>";
	}
	strpage += "</select>";
	
	var strgopage = "<select id='show_gopage' style='float:left;height:30px;width:80px'>";
	for(var i=1; i<=g_appObj.totalPage; i++){
		if(i == g_appObj.curPage)
			strgopage += "<option selected value='" +i+ "'>" +i+ "</option>";
		else
			strgopage += "<option value='" +i+ "'>" +i+ "</option>";			
	}
	strgopage += "</select>";
						

/*		var s = 
	"<table height=100% border='0'><tr>"+
   	"<td style='vertical-align:top'>共有" +g_appObj.totalSize+"个记录，当前" +g_appObj.curPage+ "/" +g_appObj.totalPage+ "页</td>" +
   	"<td style='vertical-align:top'>每页记录数</td>" +"<td style='vertical-align:top'>" +strpage+ "</td>"+
   	"<td style='vertical-align:top'><button id='show_first' style='height:30px'>首页</button></td>" +
   	"<td style='vertical-align:top'><button id='show_prev' style='height:30px'>前页</button></td>" +
   	"<td style='vertical-align:top'><button id='show_next' style='height:30px'>后页</button></td>" +
   	"<td style='vertical-align:top'><button id='show_last' style='height:30px'>尾页</button></td>" +
   	"<td style='vertical-align:top'>转到第</td>" +"<td>"+strgopage+"</td>"+"<td style='vertical-align:top'>页</td>";
	"</tr></table>";
*/
	var s = 
	"<table height=100% border='0'><tr>"+
   	"<td>共有" +g_appObj.totalSize+"个记录，当前" +g_appObj.curPage+ "/" +g_appObj.totalPage+ "页,</td>" +
   	"<td>每页记录数</td>" +"<td style='vertical-align:top'>" +strpage+ "</td>"+
   	"<td style='vertical-align:top'><button class='btn' id='show_first' style='height:30px'>首页</button></td>" +
   	"<td style='vertical-align:top'><button class='btn' id='show_prev' style='height:30px'>前页</button></td>" +
   	"<td style='vertical-align:top'><button class='btn' id='show_next' style='height:30px'>后页</button></td>" +
   	"<td style='vertical-align:top'><button class='btn' id='show_last' style='height:30px'>尾页</button></td>" +
   	"<td>转到第</td>" +"<td>"+strgopage+"</td>"+"<td>页</td>";
	"</tr></table>";
	
	
	
	return s;
}

function g_setTableMsg(){
	var obj = document.getElementById("show_first");
	g_addEvent(obj, "click", g_showFirst);
	obj = document.getElementById("show_prev");
	g_addEvent(obj, "click", g_showPrevious);
	obj = document.getElementById("show_next");
	g_addEvent(obj, "click", g_showNext);
	obj = document.getElementById("show_last");
	g_addEvent(obj, "click", g_showLast);
	obj = document.getElementById("show_pagesize");
	g_addEvent(obj, "change", g_showPageSize);	
	obj = document.getElementById("show_gopage");
	g_addEvent(obj, "change", g_showGoPage);
	obj = document.getElementById("show_table");
	g_addEvent(obj, "mouseover", g_tableMouseOver);
	g_addEvent(obj, "mouseout", g_tableMouseOut);	
}

function g_showFirst(e){
	g_appObj.curPage = 1;
	g_appObj.show(e);
}

function g_showPrevious(e){
	if(g_appObj.curPage>1)
		g_appObj.curPage --;
	g_appObj.show(e);
}

function g_showNext(e){
	if(g_appObj.curPage<g_appObj.totalPage)
		g_appObj.curPage ++;	
	g_appObj.show(e);
}

function g_showLast(e){
	g_appObj.curPage = g_appObj.totalPage;
	g_appObj.show(e);
}

function g_showPageSize(e){
	var obj = e.target || window.event.srcElement;
	g_appObj.pageSize = parseInt(obj.value);
	g_appObj.curPage = 1;
	g_appObj.show(e);
}

function g_showGoPage(e){
	var obj = e.target || window.event.srcElement;
	g_appObj.curPage = parseInt(obj.value);
	g_appObj.show(e);
}

function g_tableMouseOver(e){
	var obj = e.target || window.event.srcElement;
	if(obj.tagName != "TD")
		return;
	var trObj = obj.parentElement;
	for(var i=0; i<trObj.childNodes.length; i++){
		trObj.childNodes[i].style.backgroundColor = g_highlightcolor;
	}
}

function g_tableMouseOut(e){
	var obj = e.target || window.event.srcElement;
	if(obj.tagName != "TD")
		return;
	var trObj = obj.parentElement;
	for(var i=0; i<trObj.childNodes.length; i++){
		trObj.childNodes[i].style.backgroundColor = "";
	}
}


/**************新编制的分页显示*********************/

/*对显示接收数据的格式说明，形如
 * {
 *   'totalSize':10,	//总页数
 *   'curPage':5,		//当前页数
 *   'row':[			//数据数组
 *   	{'col':['学号','密码','姓名','电话','邮箱']},
 *      {'col':['学号','密码','姓名','电话','邮箱']},
 *      {'col':['学号','密码','姓名','电话','邮箱']}
 *   ]
 *   
 * }
 */

/**
 * 页面显示全局函数
 * @strJSON:从服务器返回的字符串，形如上方
 * @title: 表头显示数组
 * @pos: 加超链接位置数组，均加<a href='#' name='currow'>
 *       在调用方对name加消息注册
 *       pos=null表明无需加超链接
 * @iddiv:分页所在的div id号
 */
function g_displayPage(strJSON, title,pos,iddiv){
	var objJSON = g_parseJSONString(strJSON);
	g_displayPage2(objJSON, title, pos, iddiv);
	
}


function g_displayPage2(objJSON, title,pos,iddiv){
	var bPrev = true;
	var bNext = true;
	var bCur = true;
	
	var totalSize = objJSON.totalSize;
	var curPage = objJSON.curPage;
	var pages = Math.floor(totalSize/g_pageObj.pagesize);
	if(totalSize%g_pageObj.pagesize != 0)
		pages ++ ;
	
	//形成数据体
	var s = "<div class='well'>";
	s += "<table class='table table-striped'>";
	s += "<tr>";
	for(var i=0; i<title.length; i++){
		s += "<th>" +title[i]+ "</th>";
	}
	s += "</tr>";
	
	for(var i=0; i<objJSON.row.length; i++){
		s += "<tr>";
		for(var j=0; j<objJSON.row[i].col.length; j++){
			if(pos==null)
				s += "<td style='word-wrap:break-word;word-break:break-all;' width='100px';>" +objJSON.row[i].col[j] + "</td>";
			else{
				var mark = false;
				for(var u=0; u<pos.length; u++){
					if(j==pos[u]){
						mark=true;
						break;
					}
				}
				
				if(mark)
					s += "<td><a href='#' name='currow'>" +objJSON.row[i].col[j] + "</a></td>";
				else
					s += "<td>" +objJSON.row[i].col[j] + "</td>";					
			}
		}
		s += "</tr>"
	}
	
	s += "</table>"
		//形成分页按钮
		s += "<div class='pagination'>";
		s += "<ul>";
		if(curPage-g_pageObj.size>0)
			s += "<li><a href='#' id='prevunit'>上"+g_pageObj.size+"页</a></li>";
		else bPrev = false;
		
		var start = Math.floor((curPage-1)/g_pageObj.size) * g_pageObj.size+1;
		var i = 0;
		for(i=start; i<start+g_pageObj.size; i++){
			if(i>pages)
				break;
			if(i != curPage)
				s += "<li><a href='#' name='curpage'>"+i+"</a></li>";
			else
				s += "<li class='active'><a href='#' name='curpage'>" +i+ "</a></li>";			
		}
		if(i<pages)
			s += "<li><a href='#' id='nextunit'>下" +g_pageObj.size+ "页</a></li>";
		else bNext = false;
		
		if(totalSize==0)
			bCur = false;
		
		s += "</ul>";
		s += "</div>";
		
		//形成分页按钮结束
	s += "</div>";
	
	//形成数据体结束
	
	var obj = document.getElementById(iddiv);
	obj.innerHTML = s;
	
	if(bPrev)
		g_addIdEvent("prevunit","click", g_prevUnit);
	if(bNext)
		g_addIdEvent("nextunit","click", g_nextUnit);
	if(bCur)
		g_addNameEvent("curpage","click",g_curUnit);
}

function g_prevUnit(e){
	var obj =  e.target || window.event.srcElement;
	g_pageObj.curpage = (Math.floor(g_pageObj.curpage/g_pageObj.size)-1)*g_pageObj.size+1;
	g_pageObj.search();
}

function g_nextUnit(e){
	var obj =  e.target || window.event.srcElement;
	if(g_pageObj.curpage/g_pageObj.size==0) g_pageObj.curpage=11;
	else{
		g_pageObj.curpage = (Math.floor(g_pageObj.curpage/g_pageObj.size)+1)*g_pageObj.size+1;
	}
	g_pageObj.search();
}

function g_curUnit(e){
	var obj =  e.target || window.event.srcElement;
	g_pageObj.curpage = parseInt(obj.innerHTML);
	g_pageObj.search();
}



/**************新编制的分页显示结束*********************/


/****************需要进行解码及去掉"+"*****************/
/**
 * 
 */
function g_decode(text){
	var v = text.replace(/\+/g,"%20");
	var value = decodeURIComponent(v);
	
	return value;
}
/*************************************************/